<?php

namespace app\api\controller;
use think\Lang;
use think\Db;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 收藏店铺控制器
 */
class Memberfavoritesstore extends MobileMember {

    public function _initialize() {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'home/lang/'.config('default_lang').'/memberfavorites.lang.php');
    }

    /**
     * @api {POST} api/memberfavoritesstore/favorites_list 店铺收藏列表
     * @apiVersion 1.0.0
     * @apiGroup MemberFavorites
     * 
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {String} per_page 每页显示数量
     * @apiParam {String} page 当前页数
     * 
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.favorites_list  收藏列表
     * @apiSuccess {Int} result.favorites_list.fav_id  收藏ID
     * @apiSuccess {Int} result.favorites_list.fav_time  收藏时间
     * @apiSuccess {String} result.favorites_list.fav_time_text  收藏时间描述
     * @apiSuccess {Int} result.favorites_list.goods_count  商品数量
     * @apiSuccess {String} result.favorites_list.store_avatar  店铺头像名称
     * @apiSuccess {String} result.favorites_list.store_avatar_url  店铺头像完整路径
     * @apiSuccess {Int} result.favorites_list.store_collect  收藏数量
     * @apiSuccess {Int} result.favorites_list.store_id  店铺ID
     * @apiSuccess {String} result.favorites_list.store_name  店铺名称
     * @apiSuccess {Int} result.page_total  总页数
     * @apiSuccess {Boolean} result.hasmore  是否有更多 true是false否
     */
    public function favorites_list() {
        $favorites_model = model('favorites');
        $store_model = model('store');

        $favorites_list = $favorites_model->getStoreFavoritesList(array(
            'member_id' => $this->member_info['member_id'],
                ), '*', $this->pagesize);

        $store_list = array();

        $favorites_list_indexed = array();
        foreach ($favorites_list as $v) {
            $item = array();
            $item['fav_id'] = $v['fav_id'];
            $item['store_id'] = $v['store_id'];
            $item['store_name'] = $v['store_name'];
            $item['fav_time'] = $v['fav_time'];
            $item['fav_time_text'] = date('Y-m-d H:i', $v['fav_time']);

            $store = $store_model->getStoreInfoByID($v['store_id']);
            $item['goods_count'] = $store['goods_count'];
            $item['store_collect'] = $store['store_collect'];

            $item['store_avatar'] = $store['store_avatar'];
            $item['store_avatar_url'] = get_store_logo($store['store_avatar']);

            $store_list[] = $item;
        }
        $result = array_merge(array('favorites_list' => $store_list),mobile_page($favorites_model->page_info));
        ds_json_encode(10000, '', $result);
    }

    /**
     * @api {POST} api/memberfavoritesstore/favorites_add 添加店铺收藏
     * @apiVersion 1.0.0
     * @apiGroup MemberFavorites
     * 
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {String} store_id 店铺ID
     * 
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     */
    public function favorites_add() {

        $fav_id = intval(input('post.store_id'));
        if ($fav_id <= 0) {
            ds_json_encode(10001,lang('param_error'));
        }

        $favorites_model = model('favorites');

        //判断是否已经收藏
        $favorites_info = $favorites_model->getOneFavorites(array(
            'fav_id' => $fav_id, 'fav_type' => 'store',
            'member_id' => $this->member_info['member_id'],
        ));
        if (!empty($favorites_info)) {
            ds_json_encode(10001,lang('favorite_already_favorite_store'));
        }

        //判断店铺是否为当前会员所有
        $seller_info = model('seller')->getSellerInfo(array('member_id' => $this->member_info['member_id']));
        if ($fav_id == $seller_info['store_id']) {
            ds_json_encode(10001,lang('favorite_no_my_store'));
        }

        //添加收藏
        $insert_arr = array();
        $insert_arr['member_id'] = $this->member_info['member_id'];
        $insert_arr['member_name'] = $this->member_info['member_name'];
        $insert_arr['fav_id'] = $fav_id;
        $insert_arr['fav_type'] = 'store';
        $insert_arr['fav_time'] = time();
        $result = $favorites_model->addFavorites($insert_arr);

        if ($result) {
            //增加收藏数量
            $store_model = model('store');
            $store_model->editStore(array('store_collect' => Db::raw('store_collect+1')), array('store_id' => $fav_id));
            ds_json_encode(10000,lang('favorite_collect_success'),array('success' => '1'));
        } else {
            ds_json_encode(10001,lang('favorite_collect_fail'));
        }
    }

    /**
     * @api {POST} api/memberfavoritesstore/favorites_del 删除店铺收藏
     * @apiVersion 1.0.0
     * @apiGroup MemberFavorites
     * 
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {String} fav_id 主键ID
     * 
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     */
    public function favorites_del() {
        $fav_id = intval(input('post.fav_id'));
        if ($fav_id <= 0) {
            ds_json_encode(10001,lang('param_error'));
        }

        $favorites_model = model('favorites');
        $store_model = model('store');

        $condition = array();
        $condition['fav_type'] = 'store';
        $condition['fav_id'] = $fav_id;
        $condition['member_id'] = $this->member_info['member_id'];

        //判断是否已经收藏
        $favorites_info = $favorites_model->getOneFavorites($condition);
        if (empty($favorites_info)) {
            ds_json_encode(10001,lang('favorite_del_fail'));
        }

        $favorites_model->delFavorites($condition);

        $store_model->editStore(array('store_collect' => Db::raw('store_collect-1'),), array('store_id' => $fav_id, 'store_collect' => array('gt', 0),));
        ds_json_encode(10000,lang('favorite_del_success'),array('success' => '1'));
    }

}
